#include <gtest/gtest.h>
#include <vector>
#include <queue>
#include <iostream>

using namespace std;

// 1. 测试普通函数
int Factorial(int n) {
    if (n <= 0) return 1;  // 边界处理
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}

// 2. 测试类
class Stack {
public:
    void Push(int value) {
        data_.push_back(value);
    }
    
    int Pop() {
        if (IsEmpty()) throw runtime_error("Stack is empty");
        int value = data_.back();
        data_.pop_back();
        return value;
    }
    
    int Top() const {
        if (IsEmpty()) throw runtime_error("Stack is empty");
        return data_.back();
    }
    
    bool IsEmpty() const {
        return data_.empty();
    }
    
    size_t Size() const {
        return data_.size();
    }

private:
    vector<int> data_;
};

// 3. 测试装置类
class QueueTest : public testing::Test {
protected:
    // 测试套件级别设置
    static void SetUpTestSuite() {
        cout << "===== QueueTest Suite Setup =====" << endl;
    }
    
    // 测试套件级别清理
    static void TearDownTestSuite() {
        cout << "===== QueueTest Suite Cleanup =====" << endl;
    }
    
    // 每个测试用例前的设置
    void SetUp() override {
        cout << "--- SetUp for test ---" << endl;
        q_.push(1);
        q_.push(2);
        q_.push(3);
    }
    
    // 每个测试用例后的清理
    void TearDown() override {
        cout << "--- TearDown after test ---" << endl;
        // 清空队列
        while (!q_.empty()) q_.pop();
    }
    
    // 辅助函数
    int SumQueue() {
        int sum = 0;
        queue<int> temp = q_;
        while (!temp.empty()) {
            sum += temp.front();
            temp.pop();
        }
        return sum;
    }
    
    queue<int> q_;
};

// ==================== 测试用例 ====================

// 测试普通函数 - 使用 TEST 宏
TEST(FactorialTest, HandlesPositiveInput) {
    EXPECT_EQ(Factorial(1), 1);
    EXPECT_EQ(Factorial(2), 2);
    EXPECT_EQ(Factorial(3), 6);
    EXPECT_EQ(Factorial(5), 120);
}

TEST(FactorialTest, HandlesZeroInput) {
    EXPECT_EQ(Factorial(0), 1);  // 0! = 1
}

TEST(FactorialTest, HandlesNegativeInput) {
    EXPECT_EQ(Factorial(-1), 1);
    EXPECT_EQ(Factorial(-5), 1);
}

// 测试类 - 使用 TEST 宏
TEST(StackTest, IsEmptyWhenCreated) {
    Stack s;
    EXPECT_TRUE(s.IsEmpty());
    EXPECT_EQ(s.Size(), 0);
}

TEST(StackTest, PushAndPop) {
    Stack s;
    s.Push(10);
    EXPECT_FALSE(s.IsEmpty());
    EXPECT_EQ(s.Size(), 1);
    EXPECT_EQ(s.Top(), 10);
    
    s.Push(20);
    EXPECT_EQ(s.Size(), 2);
    EXPECT_EQ(s.Top(), 20);
    
    EXPECT_EQ(s.Pop(), 20);
    EXPECT_EQ(s.Size(), 1);
    EXPECT_EQ(s.Top(), 10);
    
    EXPECT_EQ(s.Pop(), 10);
    EXPECT_TRUE(s.IsEmpty());
}

// 测试装置 - 使用 TEST_F 宏
TEST_F(QueueTest, SizeCheck) {
    cout << "Running SizeCheck test" << endl;
    EXPECT_EQ(q_.size(), 3);
    q_.push(4);
    EXPECT_EQ(q_.size(), 4);
}

TEST_F(QueueTest, FrontCheck) {
    cout << "Running FrontCheck test" << endl;
    EXPECT_EQ(q_.front(), 1);
    q_.pop();
    EXPECT_EQ(q_.front(), 2);
}

TEST_F(QueueTest, SumCheck) {
    cout << "Running SumCheck test" << endl;
    EXPECT_EQ(SumQueue(), 6);  // 1+2+3
    q_.push(4);
    EXPECT_EQ(SumQueue(), 10); // 1+2+3+4
}

// ==================== 主函数 ====================
int main(int argc, char **argv) {
    // 初始化Google Test
    testing::InitGoogleTest(&argc, argv);
    
    // 运行所有测试
    return RUN_ALL_TESTS();
}